package game.gfx;

import game.AbstractGame;
import game.util.Logger;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;


public abstract class AbstractImageGeneratingGame extends AbstractGame
{
    public enum IMAGE_POSITION
    {
        CENTER, TOP_LEFT
    }
    
    protected abstract Dimension getImageSize();
    
    protected IMAGE_POSITION getImagePosition()
    {
        return IMAGE_POSITION.CENTER;
    }
    
    private Point lastStartXY = null;
    
    protected Point getStartXY()
    {
        int x = 0;
        int y = 0;
        Dimension mapSize = getImageSize();
        if (mapSize.getWidth() < getFrameSize().getWidth())
        {
            x = (getFrameSize().width - mapSize.width) / 2;
        }
        if (mapSize.getHeight() < getFrameSize().getHeight())
        {
            y = (getFrameSize().height - mapSize.height) / 2;
        }
        Point newStartXY = new Point(x, y);
        if (lastStartXY == null || lastStartXY.equals(newStartXY) == false)
        {
//            Logger.log("STARTXY: " + x + "-" + y);
            lastStartXY = newStartXY;
        }
        return newStartXY;
    }
    
    private Point lastRelativeMousePoint = null;
    
    protected Point calcRelativeMousePoint(Point point)
    {
        Point startXY = getStartXY();
        int x = point.x - startXY.x;
        int y = point.y - startXY.y;
        Point newRelativeMousePoint = new Point(x, y);
        if (lastRelativeMousePoint == null || lastRelativeMousePoint.equals(newRelativeMousePoint) == false)
        {
            Logger.trace("RELATIVE-MOUSE-POINT: ", x, "-", y);
            lastRelativeMousePoint = newRelativeMousePoint;
        }
        return newRelativeMousePoint;
    }
    
    protected void drawCenteredImage(Graphics g, Image image)
    {
//        GraphicUtil.fillRect(g, new Point(0, 0), new Dimension(getFrameSize().width, getFrameSize().height), Color.BLACK);
        Point startXY = getStartXY();
        // Logger.log("STARTXY: " + startXY);
        g.drawImage(image, startXY.x, startXY.y, null);
    }
    
    protected abstract BufferedImage getImage();
    
    @Override
    public final void render(Graphics g)
    {
        if (isReset())
        {
            GraphicUtil.clearScreen((Graphics2D) g, getFrameSize());
            GraphicUtil.renderCenterText(g, "Status: " + getStatus() + "; Info: " + getInfo(), 30, getFrameSize());
        }
        else if (isPaused())
        {
            GraphicUtil.clearScreen((Graphics2D) g, getFrameSize());
            GraphicUtil.renderCenterText(g, "PAUSE", 30, getFrameSize());
        }
        else
        {
            BufferedImage image = getImage();
            if (image != null)
            {
                if (getImagePosition() == IMAGE_POSITION.CENTER)
                {
                    drawCenteredImage(g, image);
                }
                else
                {
                    g.drawImage(image, 0, 0, null);
                }
            }
        }
    }
}
